package com.bxd.baodan.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.bxd.baodan.R;
import com.bxd.baodan.app.JsonApi;
import com.bxd.baodan.item.InsuranceItemBean.ListBean;
import com.bxd.baodan.util.CommomDialog;
import com.bxd.baodan.util.GlideUtil;
import com.bxd.baodan.util.JSONUtil;
import com.bxd.baodan.util.NetUtil;
import com.bxd.baodan.util.StringUtil;
import com.bxd.baodan.widget.ShapedImageView;

public class InsuranceActivity extends BaseActivity {

    private final int JI_JIANG_SHENG_XIAO = 1;
    private final int BAO_ZHANG_ZHONG = 2;
    private final int JI_JIANG_DAO_QI = 3;
    private final int YI_SHI_XIAO = 4;

    private TextView mTvType;
    private TextView mTvCompany;
    private TextView mTvProductName;
    private TextView mTvTime;
    private TextView mTvMoney;
    private TextView mTvUserName;
    private TextView mTvBirthday;
    private LinearLayout mLlText;
    private LinearLayout mLlImages;
    private ShapedImageView mIvImage1;
    private ShapedImageView mIvImage2;
    private String mId;

    public TextView tvSecurity;
    public TextView tvExpiring;
    public TextView tvWillTakeEffect;
    public TextView tvFailure;
    private ListBean mBean;
    private Boolean isShowEvaluation = false;
    private PopupMenu mPopupMenu;
    private View can_click, no_click;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insurance);
        //        mBean = (ManualAddInsuranceBean) getIntent().getSerializableExtra("bean");
        mId = getIntent().getStringExtra("params");
        initView();
        initData();
    }

    private void initView() {
        setActionBarTitle("保单详情");
        resetActionBarButton(R.drawable.menu, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopmenu(v);
            }
        });

        mTvType = getTextView(R.id.tv_type);
        mTvCompany = getTextView(R.id.tv_company);
        mTvProductName = getTextView(R.id.tv_product_name);
        mTvTime = getTextView(R.id.tv_time);
        mTvMoney = getTextView(R.id.tv_money);
        mTvUserName = getTextView(R.id.tv_user_name);
        mTvBirthday = getTextView(R.id.tv_birthday);
        mLlText = getLinearLayout(R.id.ll_text);
        mLlImages = getLinearLayout(R.id.ll_images);
        //        mIvImage1 = getImageView(R.params.iv_image1);
        //        mIvImage2 = getImageView(R.params.iv_image2);
        mIvImage1 = (ShapedImageView) findViewById(R.id.iv_image1);
        mIvImage2 = (ShapedImageView) findViewById(R.id.iv_image2);

        tvSecurity = (TextView) findViewById(R.id.tv_security); // 保障中
        tvExpiring = (TextView) findViewById(R.id.tv_expiring);// 到期
        tvWillTakeEffect = (TextView) findViewById(R.id.tv_will_take_effect); // 生效
        tvFailure = (TextView) findViewById(R.id.tv_failure); // 失效

        can_click = findViewById(R.id.can_click);
        no_click = findViewById(R.id.no_click);


    }

    private void initData() {

        NetUtil netUtil = new NetUtil(InsuranceActivity.this, JsonApi.SHOW_INSURANCE);

        netUtil.setParams("uiid", mId);
        netUtil.postRequest("正在加载数据...", new NetUtil.RequestStringListener() {
            @Override
            public void onComplete(String result) {

                try {
                    mBean = new Gson().fromJson(JSONUtil.getData(result),
                            new TypeToken<ListBean>() {
                            }.getType());

                    showInfo(mBean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void showInfo(final ListBean mBean) {
        String type = mBean.type;

        mTvType.setText(mBean.category.name);
        mTvCompany.setText(mBean.company.name);
        showLabel(mBean.status);
        // 2是图片 1手动
        if ("1".equals(type)) {
            mLlText.setVisibility(View.VISIBLE);


            String[] images = mBean.image;
            if (images != null && images.length > 0) {
                mLlImages.setVisibility(View.VISIBLE);
                GlideUtil.loadImage(this, images[0], mIvImage1);

                if (images.length > 1) {
                    GlideUtil.loadImage(this, images[1], mIvImage2);
                }
            } else {
                mLlImages.setVisibility(View.GONE);
            }


            mTvProductName.setText(mBean.productName);
            mTvTime.setText(mBean.expireStart + "至" + mBean.expireEnd);
            mTvMoney.setText(mBean.insuredFee + "元/年");
            mTvUserName.setText(mBean.insured);

            try {
                String bir = mBean.birthday.replace("00:00:00", "");
                mTvBirthday.setText(bir);
            } catch (Exception e) {
                e.printStackTrace();
                mTvBirthday.setText(mBean.birthday);
            }
        } else if ("2".equals(type)) {
            mLlText.setVisibility(View.GONE);
            mLlImages.setVisibility(View.VISIBLE);

            String[] images = mBean.image;
            if (images != null && images.length > 0) {
                GlideUtil.loadImage(this, images[0], mIvImage1);
            }
            if (images != null && images.length > 1) {
                GlideUtil.loadImage(this, images[1], mIvImage2);
            }
        }
        if (mBean.evaluationProgress ) {
            can_click.setVisibility(View.VISIBLE);
            no_click.setVisibility(View.GONE);
        } else {
            can_click.setVisibility(View.GONE);
            no_click.setVisibility(View.VISIBLE);
        }

        can_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtil.isEmpty(mBean.url))
                    openActivity(WebViewPager.class, new String[]{"startUrl"}, new String[]{mBean.url});
            }
        });


    }


    private void showPopmenu(View view) {
        mPopupMenu = new PopupMenu(this, view);
        mPopupMenu.getMenuInflater().inflate(R.menu.menu_insurance, mPopupMenu.getMenu());
        mPopupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.evaluation:
                        evaluation();
                        break;
                    case R.id.delete:
                        delete();
                        break;
                }
                return false;
            }
        });

        if (mBean != null && !TextUtils.isEmpty(mBean.evaluationStatus) && "1".equals(mBean.evaluationStatus) || isShowEvaluation) {
            mPopupMenu.getMenu().findItem(R.id.evaluation).setVisible(false);
        }
        mPopupMenu.show();
    }

    /**
     * 保单测评
     */
    private void evaluation() {
        new CommomDialog(InsuranceActivity.this, R.style.insurance_dialog, "保单性价比如何？精算师为您深度测评", new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    NetUtil netUtil = new NetUtil(InsuranceActivity.this, JsonApi.NEEDE_VALUATION);
                    netUtil.setParams("uiid", mId);
                    netUtil.postRequest("正在提交中...", new NetUtil.RequestStringListener() {
                        @Override
                        public void onComplete(String result) {
                            showToast("规划顾问会在3个工作日内给您电话反馈！");
                            isShowEvaluation = true;
                        }
                    });
                    dialog.dismiss();
                }
            }
        }).setTitle("预约保单测评").show();
    }

    /**
     * 删除
     */
    private void delete() {
        new CommomDialog(InsuranceActivity.this, R.style.insurance_dialog, "删除之后将不可再恢复", new CommomDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean confirm) {
                if (confirm) {
                    NetUtil netUtil = new NetUtil(InsuranceActivity.this, JsonApi.INSURANCE_DELETE);
                    netUtil.setParams("uiid", mId);
                    netUtil.postRequest("正在删除中...", new NetUtil.RequestStringListener() {
                        @Override
                        public void onComplete(String result) {
                            showToast("删除成功!");
                            InsuranceActivity.this.finish();
                        }
                    });
                    dialog.dismiss();
                }
            }
        }).setTitle("确认删除保单吗？").show();
    }

    public void showLabel(String label) {
        int index = 0;
        try {
            index = Integer.parseInt(label);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return;
        }

        tvSecurity.setVisibility(View.GONE);
        tvExpiring.setVisibility(View.GONE);
        tvWillTakeEffect.setVisibility(View.GONE);
        tvFailure.setVisibility(View.GONE);
        switch (index) {
            case JI_JIANG_SHENG_XIAO:
                tvWillTakeEffect.setVisibility(View.VISIBLE);
                break;
            case BAO_ZHANG_ZHONG:
                tvSecurity.setVisibility(View.VISIBLE);
                break;
            case JI_JIANG_DAO_QI:
                tvExpiring.setVisibility(View.VISIBLE);
                break;
            case YI_SHI_XIAO:
                tvFailure.setVisibility(View.VISIBLE);
                break;
        }
    }
}
